#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
void STInit(ST* ps) {
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

void STDestroy(ST* ps) {
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(ST* ps, STDataType x) {
	if (ps->top == ps->capacity) {
		int newCapacity = ps->capacity ? 2 * ps->capacity : 4;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL) {
			perror("realloc fail");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}

	ps->arr[ps->top++] = x;
}

bool StackEmpty(ST* ps) {
	assert(ps);
	return ps->top == 0;
}

void StackPop(ST* ps) {
	assert(ps && !StackEmpty(ps));
	ps->top--;
}

STDataType StackTop(ST* ps) {
	assert(ps && !StackEmpty(ps));
	return ps->arr[ps->top - 1];
}

int STSize(ST* ps) {
	assert(ps);
	return ps->top;
}